package intelligentrecommender.phases.htmlextractor;

import intelligentrecommender.phases.utilities.TagInfo;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.htmlparser.jericho.Config;
import net.htmlparser.jericho.LoggerProvider;
import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;

/**
 *
 * @author Eias Tawil
 */
public class HTMLparser {

    /**
     * Searching for specific HTML tags in the result folder
     *
     * @param folder The folder of search result pages
     * @param tags List of HTML tags needed
     * @return <code>Map</code> of <code>TagInfo<code> with the url
     *
     */
    public Map<String, TagInfo> getEmphasizedTagContent(String folder, List<String> tags) throws IOException {
        Config.LoggerProvider = LoggerProvider.DISABLED;
        Map<String, TagInfo> result = new HashMap<>();
        TagInfo tagInfo = null;
        File files[] = new File(folder).listFiles();//Get all the files in the directory
        String url;
        int counter = 0;

        for (File file : files) {
            url = file.toString();
            // Search form html files
            if (file.isFile() && url.endsWith(".html")) {
                // Create a cononical form for the path
                if (url.indexOf(':') == -1) {
                    url = "file:" + file.getCanonicalPath();
                }
                Source source = new Source(new URL(url));
                // Search for the given tags
                for (String tag : tags) {
                    // Return segments of the current tag
                    for (Segment segment : source.getAllElements(tag)) {
                        String tagContent = segment.getTextExtractor().toString();// Extract the text form the segment
                        //tagInfo.add(new TagInfo(url, tag, tagContent,"aa"));// Add the element to the list
                        tagInfo = new TagInfo(url, tag, tagContent, "aa");
                        result.put("" + counter++, tagInfo);// Add the element to the hash map
                        //System.out.println(tagContent);
                    }
                }
            }
        }
        return result;
    }
    
    public static void main(String[] args) throws IOException {

        List<String> tags = Arrays.asList("P");
        HTMLparser hp = new HTMLparser();

        Map<String, TagInfo> emphasizedTagContent = hp.getEmphasizedTagContent("src/intelligentrecommender/phases/htmlextractor/results/", tags);
        for (Map.Entry<String, TagInfo> entry : emphasizedTagContent.entrySet()) {
            //String key = entry.getKey();
            TagInfo ti = (TagInfo) entry.getValue();
            System.out.println(ti.getTagName() + " ## " + ti.getTagContent());
        }
    }
}
